package com.gaussian.property.admin.module.biz.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gaussian.property.admin.module.biz.vo.message.MessageForm;
import com.gaussian.property.admin.module.biz.vo.message.MessageReplyVo;
import com.gaussian.property.module.biz.entity.Message;
import com.gaussian.property.module.biz.entity.ProjectMessage;
import com.gaussian.property.module.biz.entity.ProjectMessageReply;
import com.gaussian.property.module.biz.mapper.ProjectMessageMapper;
import com.gaussian.property.module.biz.service.IMessageService;
import com.gaussian.property.module.biz.service.IProjectMessageReplyService;
import com.gaussian.property.module.biz.service.IProjectMessageService;
import com.gaussian.property.module.biz.vo.message.PageQueryMessageRequestDto;
import com.gaussian.property.module.biz.vo.message.PageQueryMessageResponse;
import com.gaussian.property.module.system.entity.SysUser;
import com.songqi.common.core.constant.Constants;
import com.songqi.common.core.utils.BeanCopyUtil;
import com.songqi.common.mybatis.core.util.MyBatisUtils;
import com.songqi.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: songqi(13776346982)
 * @date: 2024/4/19 16:56
 */
@Service
public class MessageService {

    @Resource
    private IMessageService iMessageService;
    @Resource
    private IProjectMessageService iProjectMessageService;
    @Resource
    private IProjectMessageReplyService iProjectMessageReplyService;
    @Resource
    private ProjectMessageMapper projectMessageMapper;




    /**
     * 分页查询项目留言信息
     * @param queryRequest
     * @return
     */
    public IPage<PageQueryMessageResponse> pageQueryProjectMessage(PageQueryMessageRequestDto queryRequest) {
        IPage<PageQueryMessageResponse> iPage = MyBatisUtils.buildPage(queryRequest);
        IPage<PageQueryMessageResponse> pageData = projectMessageMapper.pageQueryProjectMessage(iPage, queryRequest);
        if(CollectionUtil.isNotEmpty(pageData.getRecords())){
            for (PageQueryMessageResponse record : pageData.getRecords()) {
                record.setReplierRealNameList(getReplierRealNameList(record.getProjectMessageId()));
            }
        }
        return pageData;
    }

    private List<String> getReplierRealNameList(String projectMessageId) {
        LambdaQueryWrapper<ProjectMessageReply> queryWrapper = new LambdaQueryWrapper<ProjectMessageReply>()
                .eq(ProjectMessageReply::getProjectMessageId, projectMessageId)
                .orderByDesc(ProjectMessageReply::getCreateTime);
        List<ProjectMessageReply> replyList = iProjectMessageReplyService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(replyList)){
            return replyList.stream().distinct().map(ProjectMessageReply::getReplierRealName).collect(Collectors.toList());
        }
        return null;
    }


    /**
     * 创建留言
     * @param messageForm
     */
    @Transactional(rollbackFor = Exception.class)
    public void createMessage(MessageForm messageForm) {

        SysUser sysUser = (SysUser) SecurityUtils.getTokenUser();
        Message message = new Message();
        message.setContent(messageForm.getContent());
        message.setCreatorRealName(sysUser.getNickName());
        iMessageService.save(message);
        if(CollectionUtil.isNotEmpty(messageForm.getProjectIdList())){
            List<ProjectMessage> projectMessageList = new ArrayList<>();
            for (String projectId : messageForm.getProjectIdList()) {
                ProjectMessage projectMessage = new ProjectMessage();
                projectMessage.setProjectId(projectId);
                projectMessage.setMessageId(message.getMessageId());
                projectMessage.setIsReply(Constants.NO);

                projectMessageList.add(projectMessage);
            }
            iProjectMessageService.saveBatch(projectMessageList);

        }

    }


    public List<MessageReplyVo> queryMessageReply(String projectMessageId) {
        LambdaQueryWrapper<ProjectMessageReply> queryWrapper = new LambdaQueryWrapper<ProjectMessageReply>()
                .eq(ProjectMessageReply::getProjectMessageId, projectMessageId)
                .orderByDesc(ProjectMessageReply::getCreateTime);
        List<ProjectMessageReply> list = iProjectMessageReplyService.list(queryWrapper);
        return BeanCopyUtil.copyList(list, MessageReplyVo.class);
    }
}
